package com.example.service.impl;

import com.example.dao.EvaluateDao;
import com.example.dao.GoodsDao;
import com.example.dao.MemberDao;
import com.example.entity.Evaluate;
import com.example.entity.Goods;
import com.example.entity.Member;
import com.example.service.EvaluateService;
import com.example.util.Info;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;

/**
 * 评价服务实现类
 */
@Service
public class EvaluateServiceImpl implements EvaluateService {

    @Resource
    private EvaluateDao evaluateDao;
    
    @Resource
    private MemberDao memberDao;
    
    @Resource
    private GoodsDao goodsDao;

    @Override
    public PageInfo<Evaluate> getEvaluateList(Integer pageNum, Integer pageSize, String key, String goodsid) {
        HashMap<String, Object> map = new HashMap<>();
        map.put("key", key);
        map.put("goodsid", goodsid);
        
        PageHelper.startPage(pageNum, pageSize);
        List<Evaluate> list = evaluateDao.selectAll(map);
        
        // 填充评价关联的商品和会员信息
        for (Evaluate evaluate : list) {
            Goods goods = goodsDao.findById(evaluate.getGoodsid());
            evaluate.setGoods(goods);
            
            Member member = memberDao.findById(evaluate.getMemberid());
            evaluate.setMember(member);
        }
        
        return new PageInfo<>(list);
    }

    @Override
    public void addEvaluate(Evaluate evaluate) {
        evaluateDao.add(evaluate);
    }

    @Override
    public void replyEvaluate(Evaluate evaluate) {
        evaluate.setHftime(Info.getDateStr());
        evaluateDao.update(evaluate);
    }

    @Override
    public void deleteEvaluate(int id) {
        evaluateDao.delete(id);
    }

    @Override
    public void batchDeleteEvaluate(String vals) {
        String[] val = vals.split(",");
        for (String id : val) {
            evaluateDao.delete(Integer.parseInt(id));
        }
    }
} 